package com.huaqin.customer;

public class Address {
	private int mSendType = SEND_TYPE_UNKNOW;
	private int mAddressIndex = 0;
	private boolean isRetry = false; //发送失败时是否重试过
	private boolean isRepeatUse = false; //地址是否可以复用

    private String mAddress;
    
    private static final String PHONE_PATTERN = "[*]?[0]?1{1}[0-9]{10}";
    private static final String HTTPS_PATTERN = "[*]?http://[0-9,a-z,A-Z,:,/,+,?,&,%,#,.,-,_,=]{30,70}";
    private static final String EAMIL_PATTERN = "[*]?[0-9,a-z,A-Z,_,-]{6,30}@[0-9,a-z]{2,8}.com[.]?[c]?[n]?";
    
	//消息发送的类型
    public static final int SEND_TYPE_UNKNOW = 0;
    public static final int SEND_TYPE_SMS = 1;
    public static final int SEND_TYPE_HTTP = 2;
    public static final int SEND_TYPE_EMAIL = 3;
    
    public Address(int index, String address) {
        setAddress(address);
        setAddressIndex(index);
    }

    //Filter address start with *
    public void setAddress(String address) {
    	if(address == null){
    		return;
    	}
    	
        if(address.charAt(0) == '*'){
        	isRepeatUse = true;
        	mAddress = address.substring(1);
        }
        else{
        	mAddress = address;
        }
        
		if (mAddress.matches(PHONE_PATTERN)){
			mSendType = Address.SEND_TYPE_SMS;
		}
		else if(mAddress.matches(HTTPS_PATTERN)){
			mSendType = Address.SEND_TYPE_HTTP;
		}
		else if(mAddress.matches(EAMIL_PATTERN)){
			mSendType = Address.SEND_TYPE_EMAIL;
		}
		else{
			mSendType = Address.SEND_TYPE_UNKNOW;
		}
    }
    
    public void setAddressIndex(int index) {
        this.mAddressIndex = index;
    }
    
    public boolean isRepeatUse(){
    	return isRepeatUse;
    }
    
    public void setRetry(boolean bool){
    	isRetry = bool;
    }
    
    public boolean isRetry(){
    	return isRetry;
    }
    
    public String getAddress() {
        return mAddress;
    }
    
    public int getAddressIndex() {
        return mAddressIndex;
    }
	
	public int getSendMode(){
		return mSendType;
	}

}
